package BiTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: java_concurrent
 * @description: 输出树的右视图，也就是每一层的最右边那一个节点
 * @author: lxf
 * @create: 2024-07-24 17:16
 **/
public class RightSideView {
    List<Integer> list = new ArrayList<Integer>();
    public List<Integer> rightSideView(TreeNode root) {
        if (root == null) return list;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
                if (i == size - 1) {
                    list.add(poll.val);
                }
            }
        }
        return list;
    }
}
